#include "SpeechRecognizer.h"
#include "AudioFormat.h"
#include <stdlib.h>     /* system  definition */

SpeechRecognizer::SpeechRecognizer(SREngineType type)
{
	m_engineType    = type;
	m_pConfigurator = UIConfigResourceManager::getInstance();
	m_pLogger		= UILogger::getInstance();
	m_isInitialized = false;
	m_isRecording   = false;

	std::string paraPrefix = "sr.target." + getSREngineName(m_engineType) + ".";
	m_pConfigurator->getIntValue(MODULE_SpeechManager, "sr-audiodata", paraPrefix + "samplerate", m_targetFormat.nSampleRate);
	m_pConfigurator->getIntValue(MODULE_SpeechManager, "sr-audiodata", paraPrefix + "bits", m_targetFormat.nBits);
	m_pConfigurator->getIntValue(MODULE_SpeechManager, "sr-audiodata", paraPrefix + "channel", m_targetFormat.nChannels);
	m_pConfigurator->getStringValue(MODULE_SpeechManager, "sr-audiodata", paraPrefix + "encoding", m_targetFormat.encoding);
	m_pConfigurator->getStringValue(MODULE_SpeechManager, "sr-audiodata", paraPrefix + "endian", m_targetFormat.endian);
	m_pConfigurator->getStringValue(MODULE_SpeechManager, "sr-audiodata", paraPrefix + "type", m_targetFormat.type);

	paraPrefix = "sr.source.";
	m_pConfigurator->getIntValue(MODULE_SpeechManager, "sr-audiodata", paraPrefix + "samplerate", m_sourceFormat.nSampleRate);
	m_pConfigurator->getIntValue(MODULE_SpeechManager, "sr-audiodata", paraPrefix + "bits", m_sourceFormat.nBits);
	m_pConfigurator->getIntValue(MODULE_SpeechManager, "sr-audiodata", paraPrefix + "channel", m_sourceFormat.nChannels);
	m_pConfigurator->getStringValue(MODULE_SpeechManager, "sr-audiodata", paraPrefix + "encoding", m_sourceFormat.encoding);
	m_pConfigurator->getStringValue(MODULE_SpeechManager, "sr-audiodata", paraPrefix + "endian", m_sourceFormat.endian);
	m_pConfigurator->getStringValue(MODULE_SpeechManager, "sr-audiodata", paraPrefix + "type", m_sourceFormat.type);

	m_pConfigurator->getFileName(MODULE_SpeechManager, "sr-audiodata", "soxPath", m_soxPath);
}

SpeechRecognizer::~SpeechRecognizer(void)
{
}

void SpeechRecognizer::convertFormat(const char* srcPath, const char* desPath)
{
	//TODO: compare source and target to save time
	std::string cmd = "\"\"" + m_soxPath + "\" ";
	cmd += getAudioFormatString(m_sourceFormat) + " \"" + srcPath + "\" ";
	cmd += getAudioFormatString(m_targetFormat) + " \"" + desPath + "\"\"";

	char* CMD = NULL;
	strcpy(CMD, cmd.c_str());
	int result;
	result = system(CMD);
	if (result) return;
}
